<template>
	<view class="content">
		<view class="wrap">
			<u-swiper :list="list" :effect3d="true"></u-swiper>
			<view class="listView">
				<DailyList :flowList="listData" ref="child" />
			</view>
		</view>
	</view>
</template>

<script>
	import DailyList from '@/components/DailyList/DailyList.vue'
	import bus from '../../utils/index.js'
	export default {
		components: {
			DailyList
		},
		data() {
			return {
				list: [{
						image: '',
					},
					{
						image: 'https://img0.baidu.com/it/u=789355637,3051471630&fm=253&fmt=auto&app=138&f=JPEG?w=658&h=263',
					},
					{
						image: '',
					}
				],
				listData: [],
				pageNum: 1,
				pageSize: 10,
				show: true
			}
		},
		onLoad() {
			console.log("vuex_token:", this.vuex_token);
			if (this.vuex_token === "" || this.vuex_token === null)
				this.getData()
			else
				this.recommend()
		},
		mounted() {
			var that = this
			bus.$on('index', function(msg) {
				console.log('登录后刷新', msg);
				that.$u.vuex('vuex_data:', [])
				console.log('vuex_data:' + that.vuex_data);
				that.show = false
				that.show = true
				that.$forceUpdate()
				that.recommend()
			})

		},
		onReachBottom() {
			this.pageNum++
			let that = this
			setTimeout(() => {
				if (that.vuex_token === "" || that.vuex_token === null)
					that.getData()
				else
					that.recommend()
			}, 500)
		},
		methods: {
			async getData() {
				let params = {
					pageNum: this.pageNum,
					pageSize: this.pageSize
				}
				try {
					let res = await this.$u.api.index(params)

					let result = res.records
					console.log(result);

					if (result.length > 0) //查看是否有数据以防报错

						result.forEach(item => {
							if (item.dPicture !== null)
								item.dPicture = item.dPicture.split(",")[0]
						})
					if (res.records.length > 0)
						this.listData.push(...res.records)
					else
						this.listData = res.records
					// console.log(this.listData);
				} catch (e) {}
			},
			async recommend() {
				let res = await this.$u.api.getUserFood({
					id: this.vuex_token.openid
				})

				//数据存入数组中备用
				let arr = [
					res.bDecoct, //煎 0
					res.bFry, //炒 1
					res.bMeat, //肉 2
					res.bPlain, //菜 3
					res.bSour, //酸 4
					res.bSpicy, //辣 5
					res.bSweet //甜 6
				];
				//复制数组
				let newArr = [...arr]
				//对新数组排序
				newArr = newArr.sort((a, b) => {
					return b - a
				})

				//准备前三的变量
				let maxOne = 0
				let maxTwo = 0
				let maxThree = 0

				//遍历赋值前三排行版
				for (let i = 0; i < arr.length; i++) {
					if (newArr[0] === arr[i]) {
						maxOne = i
					}
					if (newArr[1] === arr[i]) {
						maxTwo = i
					}
					if (newArr[2] === arr[i]) {
						maxThree = i
					}
				}
				console.log(maxOne, maxTwo, maxThree);

				//准备提交的params
				let params = {
					tDecoct: 0, //煎 0
					tFry: 0, //炒 1
					tMeat: 0, //肉 2
					tPlain: 0, //菜 3
					tSour: 0, //酸 4
					tSpicy: 0, //辣 5
					tSweet: 0 //甜 6
				}

				//把前三映射入params中
				let mid = 0
				for (let key in params) {
					if (mid === maxOne) {
						params[key] = 1
					}
					if (mid === maxTwo) {
						params[key] = 1
					}
					if (mid === maxThree) {
						params[key] = 1
					}
					mid++
				}

				// console.log(params)
				res = await this.$u.api.getLikeFood(params)
				// console.log(res);
				//准备查询出来的id
				let ids = []
				res.forEach(item => {
					ids.push(item.tPid)
				})
				console.log(ids);

				//按id批量查询
				let result = await this.$u.api.listUser({
					ids: ids,
					pageNum: this.pageNum,
					pageSize: this.pageSize
				})
				console.log(result);
				result.forEach(item => {
					if (item.dPicture !== null)
						item.dPicture = item.dPicture.split(",")[0]
				})
				if (result.length > 0)
					this.listData.push(...result)
				else
					this.listData = result
			}
		}
	}
</script>

<style lang="scss" scoped>

</style>
